unit IO;

interface

uses
  Database, PlayerProfiles,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, Gauges, Menus, ComCtrls, Db, DBTables;

type
  TFormIO = class(TForm)
    BtnClose: TBitBtn;
    BtnHelp: TBitBtn;
    BtnExport: TBitBtn;
    BtnImport: TBitBtn;
    BoxItems: TGroupBox;
    RBAliases: TRadioButton;
    RBMacros: TRadioButton;
    BoxImport: TGroupBox;
    RBI1: TRadioButton;
    RBI2: TRadioButton;
    RBI3: TRadioButton;
    BoxExport: TGroupBox;
    RBE1: TRadioButton;
    RBE2: TRadioButton;
    DialogSave: TSaveDialog;
    DialogOpen: TOpenDialog;
    RBFull: TRadioButton;
    RBI4: TRadioButton;
    MainMenu: TMainMenu;
    MenuHelp: TMenuItem;
    procedure BtnExportClick(Sender: TObject);
    procedure BtnImportClick(Sender: TObject);
    procedure BtnCloseClick(Sender: TObject);
    procedure RBFullClick(Sender: TObject);
    procedure BtnHelpClick(Sender: TObject);
  private
    { Private declarations }
    function kindstring(plural: boolean): string;
    procedure FullExport;
    function CheckChoice: boolean;
  public
    { Public declarations }
  end;

var
  FormIO: TFormIO;

implementation
uses
   DBClient;

{$R *.DFM}

procedure TFormIO.BtnExportClick(Sender: TObject);
var
  adds, cmds: TStringList;
  kind, i: integer;
  PFile: TextFile;
  table: TClientDataset;
begin
  if not CheckChoice then exit;
  if RBFull.checked then begin
    FullExport;
    messagedlg('Export complete!',mtinformation,[mbok],0);
    exit;
  end;
  if RBAliases.checked then kind:=0
  else kind:=1;
  case kind of
    0: begin
         if Datawell.TableAliases.fieldbyname('Master').asinteger < 1 then begin
           messagedlg('There are no aliases to export',mtinformation,[mbok],0);
           exit;
         end;
         DialogSave.filter:='Portal Alias Packets|*.aliases';
         DialogSave.title:='Export Aliases';
       end;
    1: begin
         if Datawell.TableMacros.fieldbyname('Master').asinteger < 1 then begin
           messagedlg('There are no macros to export',mtinformation,[mbok],0);
           exit;
         end;
         DialogSave.filter:='Portal Macros Packets|*.macros';
         DialogSave.title:='Export Macros';
       end;
  end;
  DialogSave.initialdir:=Datawell.GetDir(1);
  DialogSave.filename:=Datawell.TableProfiles.fieldbyname('Player').asstring+' - '+
    Datawell.TableProfiles.fieldbyname('MUD').asstring;
  if not DialogSave.execute then abort;
  Datawell.SetDir(1,DialogSave.filename);
  adds:=TStringList.create;
  cmds:=TStringList.create;
  adds.add('DO NOT EDIT THIS FILE!');
  adds.add(Datawell.version2);
  table:=TClientDataset.create(self);
  with table do begin
    case kind of
      0: CloneCursor(Datawell.TableAliases, false);
      1: CloneCursor(Datawell.TableMacros, false);
    end;
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      cmds.assign(fieldbyname('Script'));
      if (kind = 0) or (cmds.count > 0) then begin
        if RBE2.checked then begin
          if messagedlg('Export "'+fieldbyname(kindstring(false)).asstring+'"?',
            mtconfirmation,[mbyes,mbno],0) = mrno then begin
            next;
            continue;
          end;
        end;
        adds.add(fieldbyname(kindstring(false)).asstring);
        for i:=0 to cmds.count-1 do adds.add(cmds[i]);
        adds.add('<ENDSCRIPT>');
      end;
      next;
    end;
  end;
  table.free;
  adds.add('<ENDPACKET>');
  AssignFile(PFile,DialogSave.filename);
  Rewrite(PFile);
  for i:=0 to adds.count-1 do WriteLn(PFile,adds[i]);
  CloseFile(PFile);
  adds.free;
  cmds.free;
  messagedlg('Export complete!',mtinformation,[mbok],0);
end;

procedure TFormIO.BtnImportClick(Sender: TObject);
var
  cver, version: string;
  kind, currentversion, fileversion: integer;
  table: TClientDataset;
  cmds, lines: TStringList;
  didfind, hasscript: boolean;
begin
  if not CheckChoice then exit;
  if RBAliases.checked then kind:=0
  else kind:=1;
  case kind of
    0: begin
         DialogOpen.filter:='Portal Alias Packets|*.aliases';
         DialogSave.title:='Import Aliases';
       end;
    1: begin
         DialogOpen.filter:='Portal Macros Packets|*.macros';
         DialogSave.title:='Import Macros';
       end;
  end;
  DialogOpen.initialdir:=Datawell.GetDir(1);
  if not DialogOpen.execute then exit;
  Datawell.SetDir(1,DialogOpen.filename);
//check version
  lines:=TStringList.create;
  try
    if fileExists(DialogOpen.filename) then
      lines.loadfromfile(DialogOpen.filename);
    if lines.count < 1 then begin
      messagedlg('The file is corrupt',mtinformation,[mbok],0);
      exit;
    end;
    lines.delete(0);
    version:=lines[0];
  finally
    lines.free;
  end;
  version:=Datawell.subs(FormatSettings.DecimalSeparator,'',version);
  version:=Datawell.subs('.','',version);
  version:=Datawell.subs(',','',version);
  try fileversion:=strtoint(version);
    except on EConvertError do begin
      messagedlg('The file is corrupt',mtinformation,[mbok],0);
      exit;
    end;
  end;
  cver:=Datawell.version;
  cver:=Datawell.subs(FormatSettings.DecimalSeparator,'',cver);
  cver:=Datawell.subs('.','',cver);
  cver:=Datawell.subs(',','',cver);
  currentversion:=Datawell.toint(cver);
  while copy(version,length(version),1) = '0' do version:=copy(version,1,length(version)-1);
  if (fileversion > 6300) and (fileversion < 7600) then begin
    messagedlg('Beta files can''t be imported',mtinformation,[mbok],0);
    exit;
  end;
  if fileversion > currentversion then begin
    messagedlg('You need client version '+version+' to import that file',
      mtinformation,[mbok],0);
    exit;
  end;
//Start import
  lines:=TStringList.create;
  try
    if fileExists(DialogOpen.filename) then
      lines.loadfromfile(DialogOpen.filename);
    lines.delete(0);
    lines.delete(0);
  //Parse information
    cmds:=TStringList.create;
    table:=TClientDataset.create(self);
    try
      with table do begin
        case kind of
          0: CloneCursor(Datawell.TableAliases, false);
          1: CloneCursor(Datawell.TableMacros, false);
        end;
        filtered := true;
        active:=true;
        while lines[0] <> '<ENDPACKET>' do begin
          if RBI3.checked then begin
            if (kind = 0) or (lines[1] <> '<ENDSCRIPT>') then begin
              if messagedlg('Import "'+lines[0]+'"?',
                mtconfirmation,[mbyes,mbno],0) = mrno then begin
                while lines[0] <> '<ENDSCRIPT>' do lines.delete(0);
                lines.delete(0);
                continue;
              end;
            end;
          end;
          didfind:=false;
          hasscript:=false;
          first;
          while not EOF do begin
            if fieldbyname(kindstring(false)).asstring = lines[0] then begin
              didfind:=true;
              if kind = 1 then begin
                cmds.assign(fieldbyname('Script'));
                if cmds.count > 0 then hasscript:=true;
              end;
              break;
            end;
            next;
          end;
          if didfind then begin
            if RBI2.checked then begin
              if (kind = 0) or (hasscript) then begin
                while lines[0] <> '<ENDSCRIPT>' do lines.delete(0);
                lines.delete(0);
                continue;
              end;
            end;
            if RBI4.checked then begin
              if (kind = 0) or (hasscript) then begin
                if messagedlg('Overwrite "'+fieldbyname(kindstring(false)).asstring+'"?',
                  mtconfirmation,[mbyes,mbno],0) = mrno then begin
                  while lines[0] <> '<ENDSCRIPT>' do lines.delete(0);
                  lines.delete(0);
                  continue;
                end;
              end;
            end;
            edit;
          end
          else append;
          fieldbyname('Master').asinteger:=Datawell.masterinteger;
          fieldbyname(kindstring(false)).asstring:=lines[0];
          cmds.clear;
          lines.delete(0);
          while lines[0] <> '<ENDSCRIPT>' do begin
            cmds.add(lines[0]);
            lines.delete(0);
          end;
          fieldbyname('Script').assign(cmds);
          post;
          lines.delete(0);
        end;
      end;
    finally
      cmds.free;
      table.free;
    end;
  finally
    lines.free;
  end;
  messagedlg('Import complete!',mtinformation,[mbok],0);
end;

procedure TFormIO.BtnCloseClick(Sender: TObject);
begin
  close;
end;

function TFormIO.kindstring(plural: boolean): string;
begin
  if RBAliases.checked then result:='Alias'
  else result:='Macro';
  if plural then begin
    if result = 'Alias' then result:='Aliases' else result:=result+'s';
  end;
end;

procedure TFormIO.RBFullClick(Sender: TObject);
begin
  BoxExport.enabled:=not RBFull.checked;
  BoxImport.enabled:=not RBFull.checked;
  BtnImport.enabled:=not RBFull.checked;
  if RBFull.checked then begin
    RBE1.font.color:=clgray;
    RBE2.font.color:=clgray;
    RBI1.font.color:=clgray;
    RBI2.font.color:=clgray;
    RBI3.font.color:=clgray;
    RBI4.font.color:=clgray;
  end
  else begin
    RBE1.font.color:=clwindowtext;
    RBE2.font.color:=clwindowtext;
    RBI1.font.color:=clwindowtext;
    RBI2.font.color:=clwindowtext;
    RBI3.font.color:=clwindowtext;
    RBI4.font.color:=clwindowtext;
  end;
end;

procedure TFormIO.FullExport;
var
  form: TFormProfiles;
begin
  Datawell.SetupTables2(Datawell.TableEvents,Datawell.masterindex);
  form:=TFormProfiles.create(self);
  form.silently:=true;
  form.BtnExport.click;
  form.free;
end;

procedure TFormIO.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('io');
end;

function TFormIO.CheckChoice: boolean;
begin
  result:=true;
  if (not RBAliases.checked) and  (not RBMacros.checked) and (not RBFull.checked) then begin
    messagedlg('Please choose an item',mtinformation,[mbok],0);
    result:=false;
  end;
end;

end.
